// pages/draftDetail.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import api from '../../utils/api';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uuid:'',
    draftID:'',
    oppUnitName:'',
    oppUnitMoney:'',
    remarks:'',
    clicknext: false, //点击下一步
  },
   // 对方单位
  onfieldChange(e) {
    console.log(e.detail,'对方单位')
    this.setData({
      oppUnitName:e.detail
    })
  },
   // 付款金额
  onfieldChangeMoney(e){
    console.log(e.detail,'付款金额')
    this.setData({
      oppUnitMoney:e.detail
    })
  },
    //备注
  onremarksChange(e) {
      this.setData({
        remarks: e.detail,
      });
  },
  //点击取消 返回到上一页
  cancel() {
    wx.navigateBack({
      delta: 1
    });
  },
  clicknext(){
    if(this.data.oppUnitName == ''){
      wx.showToast({
        title: '对方单位名称不能为空',
        icon: 'none'
      })
    }else if(this.data.oppUnitMoney == ''){
      wx.showToast({
        title: '付款金额不能为空',
        icon: 'none'
      })
    }else{
       let that = this
      api.draftApplicationnext({
      method: 'post',
      data: {
        dfdwmc: that.data.oppUnitName,
        remarks: that.data.remarks,
        money:that.data.oppUnitMoney
      },
      success: function (res) {
        console.log(res.data.data, "点击下一步创建汇票申请")
        if (res.data.code == 200) {
          that.getDate()
          that.setData({
            code: res.data.data.code,
            draftID: res.data.data.id,
            uuid: res.data.data.uuid,
            dfdwmc: res.data.data.dfdwmc,
            remarks:res.data.data.remarks,
            oppUnitMoney:res.data.data.money
          })
        }
      }
      })
      // 提交之后开启下一步
      that.setData({
        clicknext: true
      })
    }
    
  },
  addoppUnit(){
    wx.navigateTo({
      url: '/pages/addoppUnit/addoppUnit?id='+this.data.draftID,
    })
  },
  save(){
    let that = this
    console.log(that.data.remarks, that.data.oppUnitMoney,'4454555555')
    api.draftApplicationSave({
      method: 'post',
      data: {
        id:that.data.draftID,
        dfdwmc: that.data.oppUnitName,
        remarks: that.data.remarks,
        money: that.data.oppUnitMoney,
      },
      success: function (res) {
        console.log(res.data.data, "点击保存/更新")
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          })
        }else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
    })
  },
  sub(){
    this.save()
    setTimeout(() => {
     console.log('点击提交')
    if (this.data.dfdwmc =='') {
      wx.showToast({
        title: '请输入对方单位名称',
        icon: 'none'
      })
    }else{
      let that = this;
      api.draftApplicationSub({
      method: 'post',
      data: {
        fkId: that.data.draftID,
        uuid: that.data.uuid,
      },
      success: function (res) {
        console.log(res.data.data, "invEdit")
        if (res.data.code == 200) {
          wx.showToast({
            title: '提交成功',
            icon: 'success'
          })
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
      })
    } 
    },500)
    
  },
  addComma(date) {
    let str = '';
    let num = '';
    let newStr = '';
    date = date + '';
    if (date.indexOf('.') != -1) {
      num = date.split('.')[0]
      str = date.split('.')[1]
    } else {
      num = date
    }
    newStr = num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
    newStr = str == '' ? newStr : newStr + '.' + str;
    return newStr;
  },
  getDate() {
    // console.log(this.data.draftID,'/////////////////////')
    if (this.data.draftID) {
      let that = this
      api.draftgetFkDetail({
        data: {
          id: this.data.draftID
        },
        success: function (res) {
          console.log(res.data.data, "汇票付款申请 详情")
          if (res.data.code == 200) {
            if (res.data.data.fkDraftClaimList.length > 0) {
              // that.setData({
              //   sumMoney: that.addComma(Number(res.data.data.ycMoney) + Number(res.data.data.scMoney))
              // })
              for (let i = 0; i < res.data.data.fkDraftClaimList.length; i++) {
                res.data.data.fkDraftClaimList[i].money = that.addComma(res.data.data.fkDraftClaimList[i].money)
              }
              res.data.data.scMoney = that.addComma(res.data.data.scMoney)
              res.data.data.ycMoney = that.addComma(res.data.data.ycMoney)
              res.data.data.totalMoney = that.addComma(res.data.data.totalMoney)
            }
            that.setData({
              fkDraftClaimList:res.data.data.fkDraftClaimList,
              remarks:res.data.data.remarks,
              dfdwmc:res.data.data.dfdwmc,
              scMoney:res.data.data.scMoney,
              ycMoney:res.data.data.ycMoney,
              totalMoney:res.data.data.totalMoney,
            })
          }
        }
      })
    }
  },
  //汇票确认 （状态 已确认 未确认）
  hpqueren(e){
    console.log(e,"点击确认")
      let that = this
      api.hpqueren({
        data:{
          id: e.currentTarget.dataset.id,
        },
        success:function(res){
          console.log(res.data.data, "确认 okkk")
          if (res.data.code == 200) {
            that.getDate()
          }
        }
      })
    },
   //汇票取消
   hpqixiao(e){
    console.log(e,"点击取消")
      let that = this
      api.hpquxioa({
        data:{
          id: e.currentTarget.dataset.id,
        },
        success:function(res){
          console.log(res.data.data, "取消 okkk")
          if (res.data.code == 200) {
            that.getDate()
          }
        }
      })
   },
  //删除票据list
  delpiaoju(e) {
    let index = e.currentTarget.dataset.index;
    let that = this
    Dialog.confirm({
        message: '确定删除吗？',
      })
      .then(() => {
        api.drafdelpiaoju({
          data: {
            id: e.currentTarget.dataset.id,
          },
          success: function (res) {
            if (res.data.code == 200) {
              that.getDate()
              console.log('删除票据list')
              that.data.fkDraftClaimList.splice(index, 1)
              that.setData({
                fkDraftClaimList: that.data.fkDraftClaimList
              })
              wx.showToast({
                title: '删除成功',
                icon: 'success'
              })
            }
          }
        })
      })
      .catch(() => {
        // on cancel
        console.log('取消操作')
      });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDate()
    this.data.unitId = wx.getStorageSync('userInfo').unitId
    let that = this;
    //获取用户部门
    api.invNumber({
      data: {
        userId: this.data.unitId,
      },
      success: function (res) {
        if (res.data.code = 200) {
          that.setData({
            userName: res.data.data.userName,
            deptName: res.data.data.deptName,
            deptId: res.data.data.deptId
          })
        }
      }
    })
    // 页面的回显
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})